﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using Strongshell.Recoil.Core.Composition.Parsing.DependencyInjection;

namespace Strongshell.Recoil.Core.Composition.Parsing.Handlers
{
    public class LambdaExpressionHandler : IExpressionHandler
    {
        private readonly IExpressionHandler compilingHandler;

        public LambdaExpressionHandler(IExpressionHandler compilingHandler)
        {
            this.compilingHandler = compilingHandler;
        }

        public void Handle(IDependencyInjector currentInjector, Expression currentExpression)
        {
            if (typeof(Delegate).IsAssignableFrom(currentInjector.ExpectedType)
                && currentInjector.ExpectedType == currentExpression.Type)
            {
                var lambdaExpression = (LambdaExpression)currentExpression;
                currentInjector.Inject(lambdaExpression.Compile());
            }
            else
            {
                compilingHandler.Handle(currentInjector, currentExpression);
            }
        }
    }
}
